package com.ruoyi.web.controller.api.user;

import com.ruoyi.common.constant.PublishConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/21 14:37
 * @Version: 1.0
 */

@Validated
@Api(value = "用户-api", tags = {"前端-咨询师"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/adviser")
public class AdviserApiController {
    private final ISysUserService userService;

    @ApiOperation(value = "咨询师入驻")
    @PostMapping("/add-user")
    public R<String> addUser(@RequestBody SysUser user) {
        userService.insertFont(user);
        return R.ok("新增成功！");
    }

    @ApiOperation(value = "推荐咨询师")
    @GetMapping("/recommend-user")
    public R<TableDataInfo<SysUser>> recommendUser(SysUser user, PageQuery pageQuery) {
        user.setIsRecommend(PublishConstants.COMMON_YES);
        user.setUserType(UserType.ASK_USER.getUserType());
        return R.ok(userService.selectPageRecommendUserList(user, pageQuery));
    }

    @ApiOperation(value = "通用-咨询师")
    @GetMapping("/common-user")
    public R<TableDataInfo<SysUser>> commonUser(SysUser user, PageQuery pageQuery) {
        user.setUserType(UserType.ASK_USER.getUserType());
        return R.ok(userService.selectPageRecommendUserList(user, pageQuery));
    }

    @ApiOperation("公益专家列表")
    @GetMapping("/list")
    public TableDataInfo<SysUser> list(SysUser user, PageQuery pageQuery) {
        user.setUserType("2");
        user.setWelfareSwitch(PublishConstants.COMMON_YES);
        return userService.selectPublicList(user, pageQuery);
    }

    @ApiOperation("公益援助数据看板")
    @GetMapping("/help-kanban")
    public R<Map<String, Object>> helpKanban() {
        Map<String, Object> res = new HashMap<>();
        // 公益咨询师
        List<SysUser> users = userService.selectHelpPublicList();

        int helpPeopleSum = users.stream().mapToInt(SysUser::getHelpPeople).sum();
        double freeSum = users.stream().mapToDouble(SysUser::getFreeTime).sum();
        // 返回公益救助榜数据看板
        res.put("updateTime", LocalDateTime.now());
        res.put("helpPeople", helpPeopleSum);
        res.put("freeTime", freeSum);
        res.put("people", users.size());

        return R.ok(res);
    }

    @ApiOperation("我的代办")
    @GetMapping("/toDo")
    public R<Map<String, Object>> toDo() {
        return R.ok(this.userService.toDo());
    }
}
